home *** CD-ROM | disk | FTP | other *** search
/ Clickx 96 / Clickx 96.iso / software / tools / tool / xbmc-10.1.exe / addons / script.module.pil / lib / PIL / ImageShow.py < prev    next >
Encoding:
Python Source  |  2009-03-29  |  4.3 KB  |  164 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # im.show() drivers
  6. #
  7. # History:
  8. # 2008-04-06 fl   Created
  9. #
  10. # Copyright (c) Secret Labs AB 2008.
  11. #
  12. # See the README file for information on usage and redistribution.
  13. #
  14.  
  15. import Image
  16. import os, sys
  17.  
  18. _viewers = []
  19.  
  20. def register(viewer, order=1):
  21.     try:
  22.         if issubclass(viewer, Viewer):
  23.             viewer = viewer()
  24.     except TypeError:
  25.         pass # raised if viewer wasn't a class
  26.     if order > 0:
  27.         _viewers.append(viewer)
  28.     elif order < 0:
  29.         _viewers.insert(0, viewer)
  30.  
  31. ##
  32. # Displays a given image.
  33. #
  34. # @param image An image object.
  35. # @param title Optional title.  Not all viewers can display the title.
  36. # @param **options Additional viewer options.
  37. # @return True if a suitable viewer was found, false otherwise.
  38.  
  39. def show(image, title=None, **options):
  40.     for viewer in _viewers:
  41.         if viewer.show(image, title=title, **options):
  42.             return 1
  43.     return 0
  44.  
  45. ##
  46. # Base class for viewers.
  47.  
  48. class Viewer:
  49.  
  50.     # main api
  51.  
  52.     def show(self, image, **options):
  53.  
  54.         # save temporary image to disk
  55.         if image.mode[:4] == "I;16":
  56.             # @PIL88 @PIL101
  57.             # "I;16" isn't an 'official' mode, but we still want to
  58.             # provide a simple way to show 16-bit images.
  59.             base = "L"
  60.             # FIXME: auto-contrast if max() > 255?
  61.         else:
  62.             base = Image.getmodebase(image.mode)
  63.         if base != image.mode and image.mode != "1":
  64.             image = image.convert(base)
  65.  
  66.         self.show_image(image, **options)
  67.  
  68.     # hook methods
  69.  
  70.     format = None
  71.  
  72.     def get_format(self, image):
  73.         # return format name, or None to save as PGM/PPM
  74.         return self.format
  75.  
  76.     def get_command(self, file, **options):
  77.         raise NotImplementedError
  78.  
  79.     def save_image(self, image):
  80.         # save to temporary file, and return filename
  81.         return image._dump(format=self.get_format(image))
  82.  
  83.     def show_image(self, image, **options):
  84.         # display given image
  85.         return self.show_file(self.save_image(image), **options)
  86.  
  87.     def show_file(self, file, **options):
  88.         # display given file
  89.         os.system(self.get_command(file, **options))
  90.         return 1
  91.  
  92. # --------------------------------------------------------------------
  93.  
  94. if sys.platform == "win32":
  95.  
  96.     class WindowsViewer(Viewer):
  97.         format = "BMP"
  98.         def get_command(self, file, **options):
  99.             return "start /wait %s && del /f %s" % (file, file)
  100.  
  101.     register(WindowsViewer)
  102.  
  103. elif sys.platform == "darwin":
  104.  
  105.     class MacViewer(Viewer):
  106.         format = "BMP"
  107.         def get_command(self, file, **options):
  108.             # on darwin open returns immediately resulting in the temp
  109.             # file removal while app is opening
  110.             command = "open -a /Applications/Preview.app"
  111.             command = "(%s %s; sleep 20; rm -f %s)&" % (command, file, file)
  112.             return command
  113.  
  114.     register(MacViewer)
  115.  
  116. else:
  117.  
  118.     # unixoids
  119.  
  120.     def which(executable):
  121.         path = os.environ.get("PATH")
  122.         if not path:
  123.             return None
  124.         for dirname in path.split(os.pathsep):
  125.             filename = os.path.join(dirname, executable)
  126.             if os.path.isfile(filename):
  127.                 # FIXME: make sure it's executable
  128.                 return filename
  129.         return None
  130.  
  131.     class UnixViewer(Viewer):
  132.         def show_file(self, file, **options):
  133.             command, executable = self.get_command_ex(file, **options)
  134.             command = "(%s %s; rm -f %s)&" % (command, file, file)
  135.             os.system(command)
  136.             return 1
  137.  
  138.     # implementations
  139.  
  140.     class DisplayViewer(UnixViewer):
  141.         def get_command_ex(self, file, **options):
  142.             command = executable = "display"
  143.             return command, executable
  144.  
  145.     if which("display"):
  146.         register(DisplayViewer)
  147.  
  148.     class XVViewer(UnixViewer):
  149.         def get_command_ex(self, file, title=None, **options):
  150.             # note: xv is pretty outdated.  most modern systems have
  151.             # imagemagick's display command instead.
  152.             command = executable = "xv"
  153.             if title:
  154.                 # FIXME: do full escaping
  155.                 command = command + " -name \"%s\"" % title
  156.             return command, executable
  157.  
  158.     if which("xv"):
  159.         register(XVViewer)
  160.  
  161. if __name__ == "__main__":
  162.     # usage: python ImageShow.py imagefile [title]
  163.     print show(Image.open(sys.argv[1]), *sys.argv[2:])
  164.